﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IISLogReader
{
    public class LogRowTyped
    {
        LogRow _row;

        public LogRowTyped(LogRow row, LogHeader header)
        {
            _row = row;
            _date = GetDateValue("date");
            _time = GetValue("time");
            _siteName = GetValue("s-sitename");
            _computerName = GetValue("s-computername");
            _serverIP = GetValue("s-ip");
            _method = GetValue("cs-method");
            _uriStem = GetValue("cs-uri-stem");
            _uriQuery = GetValue("cs-uri-query");
            _port = GetValue("s-port");
            _username = GetValue("cs-username");
            _clientIP = GetValue("c-ip");
            _httpVersion = GetValue("cs-version");
            _userAgent = GetValue("cs(User-Agent)");
            _cookie = GetValue("cs(Cookie)");
            _referer = GetValue("cs(Referer)");
            _clientHost = GetValue("cs-host");
            _status = GetIntValue("sc-status");
            _subStatus = GetIntValue("sc-substatus");
            _win32Status = GetIntValue("sc-win32-status");
            _serverBytes = GetIntValue("sc-bytes");
            _clientToServerBytes = GetIntValue("cs-bytes");
            _timetaken = GetIntValue("time-taken");
            _fileName = header.FileName;
            _header = header;
        }


        private LogHeader _header;

        public LogHeader Header
        {
            get { return _header; }
            set { _header = value; }
        }
        private string _fileName;

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        public LogRow OriginalRow
        {
          get { return _row; }
          set { _row = value; }
        }

        DateTime _date = DateTime.MinValue;

        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }
        string _time;

        public string Time
        {
            get { return _time; }
            set { _time = value; }
        }
        string _siteName;

        public string SiteName
        {
            get { return _siteName; }
            set { _siteName = value; }
        }
        string _computerName;

        public string ComputerName
        {
            get { return _computerName; }
            set { _computerName = value; }
        }
        string _serverIP;

        public string ServerIP
        {
            get { return _serverIP; }
            set { _serverIP = value; }
        }
        string _method;

        public string Method
        {
            get { return _method; }
            set { _method = value; }
        }
        string _uriStem;

        public string UriStem
        {
            get { return _uriStem; }
            set { _uriStem = value; }
        }
        string _uriQuery;

        public string UriQuery
        {
            get { return _uriQuery; }
            set { _uriQuery = value; }
        }
        string _port;

        public string Port
        {
            get { return _port; }
            set { _port = value; }
        }
        string _username;

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }
        string _clientIP;

        public string ClientIP
        {
            get { return _clientIP; }
            set { _clientIP = value; }
        }
        string _httpVersion;

        public string HttpVersion
        {
            get { return _httpVersion; }
            set { _httpVersion = value; }
        }
        string _userAgent;

        public string UserAgent
        {
            get { return _userAgent; }
            set { _userAgent = value; }
        }
        string _cookie;

        public string Cookie
        {
            get { return _cookie; }
            set { _cookie = value; }
        }
        string _referer;

        public string Referer
        {
            get { return _referer; }
            set { _referer = value; }
        }
        string _clientHost;

        public string ClientHost
        {
            get { return _clientHost; }
            set { _clientHost = value; }
        }
        int _status;

        public int Status
        {
            get { return _status; }
            set { _status = value; }
        }
        int _subStatus;

        public int SubStatus
        {
            get { return _subStatus; }
            set { _subStatus = value; }
        }
        int _win32Status;

        public int Win32Status
        {
            get { return _win32Status; }
            set { _win32Status = value; }
        }
        int _serverBytes;

        public int ServerBytes
        {
            get { return _serverBytes; }
            set { _serverBytes = value; }
        }
        int _serverToClientBytes;

        public int ServerToClientBytes
        {
            get { return _serverToClientBytes; }
            set { _serverToClientBytes = value; }
        }
        int _clientToServerBytes;

        public int ClientToServerBytes
        {
            get { return _clientToServerBytes; }
            set { _clientToServerBytes = value; }
        }
        int _timetaken;

        public int Timetaken
        {
            get { return _timetaken; }
            set { _timetaken = value; }
        }
        private string GetValue(string fieldName)
        {
            string rteval = null;
            if (_row != null && _row.Fields.ContainsKey(fieldName))
            {
                rteval = _row.Fields[fieldName].FieldValue;
            }
            return rteval;
        }
        private int GetIntValue(string fieldName)
        {
            int rteval = 0;
            if (_row != null && _row.Fields.ContainsKey(fieldName))
            {
                int.TryParse(_row.Fields[fieldName].FieldValue, out rteval);
            }
            return rteval;
        }
        private DateTime GetDateValue(string fieldName)
        {
            DateTime retval;
            if (!DateTime.TryParse(GetValue("date"), out retval)) 
            {
               retval = DateTime.MinValue;
            }
            return retval;
        }
    }
}
